package com.tcm.doctor.web.controller;

import cn.hutool.core.bean.BeanUtil;
import com.base.core.dozer.DozerConvert;
import com.base.core.exception.ServiceException;
import com.base.core.utils.R;
import com.tcm.prescribe.api.domain.dto.FrequentItemDTO;
import com.tcm.prescribe.api.domain.vo.FrequentItemVO;
import com.tcm.prescribe.api.service.FrequentItemService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/doctor")
@RestController
public class FrequentItemController {

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, check = false, retries = 3)
    private FrequentItemService frequentItemService;

    /**
     * 查询常用方名称
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/freqitem/view")
    public R view(@RequestParam("id") Long id){
        try{
           FrequentItemDTO frequentItemDTO = frequentItemService.findById(id);
           FrequentItemVO frequentItemVO = new FrequentItemVO();
           BeanUtil.copyProperties(frequentItemDTO, frequentItemVO);
           return R.success(frequentItemVO);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    /**
     * //保存常用方名称
     * @param frequentItemDto
     * @return
     */
    @PostMapping("/freqitem/save")
    public R create(@RequestBody FrequentItemDTO frequentItemDto) {
        try{
            frequentItemService.save(frequentItemDto);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        } catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 更新常用方名称
     * @param frequentItemDto
     * @return
     * @throws Exception
     */
    @PostMapping("/freqitem/update")
    public R update(@RequestBody FrequentItemDTO frequentItemDto) {
        try{
            frequentItemService.update(frequentItemDto);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        } catch(Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 删除常用方
     * @param id
     * @return
     */
    @GetMapping("/freqitem/delete")
    public R delete(@RequestParam("id") Long id){
        try{
            frequentItemService.deleteLogicById(id);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        } catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 分页查询常用方
     * @param frequentId
     * @return
     */
    @GetMapping("/freqitem/list")
    public R listByFrequentId(@RequestParam("frequentId") Long frequentId){
        List<FrequentItemDTO> frequentItemDTOList = frequentItemService.listItemByFrequentId(frequentId);
        List<FrequentItemVO> frequentItemVOS = DozerConvert.copyList(frequentItemDTOList, FrequentItemVO.class);
        return R.success(frequentItemVOS);
    }
}

